package com.mvp.shq.washer.ui.mine;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jaeger.library.StatusBarUtil;
import com.mvp.shq.washer.R;
import com.mvp.shq.washer.base.BaseFragment;
import com.mvp.shq.washer.entity.User;
import com.mvp.shq.washer.rxbus.RxBus;
import com.mvp.shq.washer.rxbus.Subscribe;
import com.mvp.shq.washer.ui.coupon.CouponActivity;
import com.mvp.shq.washer.ui.wallet.WalletActivity;
import com.mvp.shq.washer.utils.EventCode;
import com.mvp.shq.washer.utils.JumpUtil;
import com.mvp.shq.washer.utils.SpUtil;
import com.trello.rxlifecycle2.LifecycleTransformer;
import com.trello.rxlifecycle2.android.FragmentEvent;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import io.reactivex.Observable;


/**
 * A simple {@link Fragment} subclass.
 */
public class MineFragment extends BaseFragment<MinePresenter, MineModel> implements MineConstract.View {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.tv_username)
    TextView tvUsername;
    @BindView(R.id.tv_balance)
    TextView tvBalance;
    @BindView(R.id.img_user)
    ImageView imgUser;
    @BindView(R.id.tv_wallet_money)
    TextView tvWalletMoney;
    @BindView(R.id.tv_new_count)
    TextView tvNewsCount;
    private User user;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_mine;
    }

    @Override
    public void initView() {
        back.setVisibility(View.GONE);
        setUserInfo();
    }
    @Subscribe(code = EventCode.SET_UNREAD_MSG)
    public void setUnread(Integer num){
        if (num > 0){
            tvNewsCount.setVisibility(View.VISIBLE);
        }else {
            tvNewsCount.setVisibility(View.GONE);
        }
    }

    @Subscribe(code = EventCode.LOGIN_OUT)
    public void loginOut(){
        setUserInfo();
    }

    @OnClick({R.id.tv_username,R.id.all_wallet,R.id.all_shop, R.id.all_coupon, R.id.all_msg, R.id.all_setting, R.id.all_share, R.id.all_link,R.id.arl_userinfo})
    public void onViewClicked(View view) {
        if (view.getId() != R.id.all_shop && user == null){
            JumpUtil.toLogin(context);
            return;
        }
        switch (view.getId()) {
            case R.id.all_wallet:
                Intent intent = new Intent(context, WalletActivity.class);
                startActivityForResult(intent,88);
                break;
            case R.id.all_shop:
                JumpUtil.toMall(context);
                break;
            case R.id.all_coupon:
                JumpUtil.toCoupon(context, CouponActivity.CHECK);
                break;
            case R.id.all_msg:
                JumpUtil.toNews(context);
                break;
            case R.id.all_setting:
                JumpUtil.toSetting(context);
                break;
            case R.id.all_share:
                JumpUtil.toShare(context);
                break;
            case R.id.all_link:
                JumpUtil.toContactUs(context);
                 break;
            case R.id.arl_userinfo:
                JumpUtil.toUserCenter(context);
                break;
        }
    }
    @Subscribe(code = EventCode.UPDATE_USER)
    public void setUserInfo(){
        int num = SpUtil.getUnReadNum();
        if (num > 0){
            tvNewsCount.setVisibility(View.VISIBLE);
        }else {
            tvNewsCount.setVisibility(View.GONE);
        }
        user = SpUtil.getUser();
        if (user == null){
            tvUsername.setText("点击登录");
            tvBalance.setText("秒速登录，体验更多功能");
            tvWalletMoney.setText("0元");
            return;
        }
        imageUtil.loadCircleImage(user.getFaceurl(),imgUser,R.mipmap.my_headshot,R.mipmap.my_headshot);
        if (!TextUtils.isEmpty(user.getNickname())){
            tvUsername.setText(user.getNickname());
        }else if (!TextUtils.isEmpty(user.getMobile())){
            tvUsername.setText(user.getNickname());
        }
        if (user.getRechargeBalance() != null){
            tvBalance.setText(user.getRechargeBalance().add(user.getGivenMoney())+"元");
            tvWalletMoney.setText(user.getRechargeBalance().add(user.getGivenMoney())+"元");
        }
    }

}
